package com.alice.springcloud.controller;

import com.alice.springcloud.pojo.Dept;
import com.alice.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

//提供restful服务
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    //获取相关的配置信息
    @Autowired
    private DiscoveryClient discoveryClient;

    @PostMapping("/dept/add")
    public boolean addDept(Dept dept) {
        return deptService.addDept(dept);
    }

    @GetMapping("/dept/get/{id}")
    public Dept get(@PathVariable("id") Long id) {
        return deptService.queryById(id);
    }

    @GetMapping("/dept/list")
    public List<Dept> getAll() {
        return deptService.queryAll();
    }

    //注册进来的微服务，获取一些消息
    @GetMapping("/dept/discovery")
    public Object discovery() {
        //获取所有清单
        List<String> instances = discoveryClient.getServices();

        //得到一个具体的微服务信息，通过id，applicationName;
        List<ServiceInstance> instances1 = discoveryClient.getInstances("springcloud-provider-8001");

        for (ServiceInstance i : instances1) {
            System.out.println(
                    i.getHost() + i.getPort() + i.getUri() + i.getServiceId() + i.getMetadata()
            );
        }
        return this.discoveryClient;
    }
}
